


$(function() {


	////
	option_menu(); //菜单函数

	show_menu(0); //默认显示首页

	//////////////////////
	//获取最新消息
	getInformation(10);

	if (typeof location.search) {
		var value = location.search;
		if (value) {
			var statue = value.split('=')[1];
			if (statue == 1) { //该用户不存在
				alert('该用户不存在!');
			} else if (statue == 2) {
				//密码错误
				alert('密码错误!');
			}else if(statue == 0){
				//异步获取用户信息
				$.ajax({
					url : 'GetUserAccount',
					type : "post",
					success : function(data) {
						if(data){
							var html = "<li>用户级别：" + data.privalege + "</li>" + "<li>账号："
							+ data.account + "</li>" + "<li>登陆时间：</li>" + "<li>"
							+ formatTime(new Date()) + "</li>"
							+ "<li style='margin-bottom:5px;'><a href='logout'style='color:blue;'>退 出</a></li>";
							$('#ul_li').html(html);
							$('#huiyuan').hide();
							$('#yonghu').show();
						}else{
							location = 'index.html';
						}

					},
					error:function(statue){
						location = 'index.html';
					}
				});
			}
		} 

	}
});

//获取最新动态,获取九条信息 
function getInformation(shu){

	//异步获取最新消息
	$.ajax({
		url :'GetNewInformation',
		type : "post",
		data:"account="+shu,
		success : function(data) {

			if(!data){
				alert('没有获取到最新消息！');
				return;
			}
			var html='';
			for(var m in data){

				var message=data[m];
				var title=message.title;
				if(title.length>20){

					title=title.substring(0,20)+'...';
				}

				html +="<tr onMouseMove=this.bgColor='#D6DEE6' onMouseOut=this.bgColor='' height='22' ><td width='70%'>" +
				"<a href='javascript:void(0);' onclick='return show_information("+message.id+");'>"+title+"</a></td>"+
				"<td width='30%' align='center'>"+formatTime(new Date(message.time))+"</td></tr>";

			}

			$('#new_information').html(html);
		}
	});
}

//显示单个消息函数
function show_information(id){
	$('#right_').html(''); //清空

	$.ajax({
		url : 'getSingleMessage',
		type : "post",
		data : "id=" + id,
		datatype : "json",
		success : function(data) {

			var f='';
			if(data.file.substring(data.file.indexOf('.')+1) == '.rar'){
				f = 'Images/rar.png';
			}else{
				f = 'Images/doc.png';
			}

			var html="<p id='title'>"+data.title+"</p>"+
			"<p id='zh'>关键字:&nbsp;&nbsp;<span id='keywords'>"+data.keywords+
			"</span> <span id='time'>"+formatTime(new Date(data.time))+"</span></p>"+
			"<div id='shuoming'>"+data.memo+"</div>"+
			"<p id='file_p'>"+
			"<span><font size='4' color='blue'>附件:</font> <a href='javascript:void(0);' id='preview_file'>"+
			"<img src='"+f+"' id='file_image' style='border: none;' />" +
			"<span id='file'>"+data.file+"</span></a></span> " +
			"&nbsp; <a href='uploadFile\\"+data.file+"' id='download'> " +
			"<img alt='' src='Images/down_image.gif' style=‘cursor: poninter; border: none;’ />" +
			"下载</a></p>";

			$('#right_').html(html); //显示消息
		},
		error:function(statue){
			alert('获取单个消息失败！');
		}
	});


	return false;
}

//格式化时间
function formatTime(time) {
	//将timestamp转换格式
	var date =time;
	var year = date.getYear() + 1900; //得到年份
	var month = date.getMonth() + 1; //得到月份
	var day = date.getDate(); //得到日期
	var hours = date.getHours(); //得到小时
	var minutes = date.getMinutes(); //得到分钟
	var seconds = date.getSeconds(); //得到秒钟

	if (month < 10) {
		month = '0' + month;
	}
	if (day < 10) {
		day = '0' + day;
	}
	if (hours < 10) {
		hours = '0' + hours;
	}
	if (minutes < 10) {
		minutes = '0' + minutes;
	}
	if (seconds < 10) {
		seconds = '0' + seconds;
	}
	var publishTime = year + '年' + month + '月' + day + '日 ' + hours + ':'
	+ minutes + ':' + seconds; //拼凑时间

	return publishTime;
}

function sub() {
	var username = $("#textfield");
	var password = $("#textfield2");
	var flag = true;
	if (Trim(username.val()) == "" || username.val() == null) {
		alert("账号不能为空！！");
		flag = false;
	} else if (Trim(password.val()) == "" || password.val() == null) {
		alert("密码不能为空！！");
		flag = false;
	}
	if (flag) {
		$('#form').submit(); //提交表单
	}
	return flag;
}

function key(evt) {
	if (evt.keyCode == 13) {
		return sub();
	}
}
///去掉字符串前尾的空格
function Trim(str) {
	return str.replace(/(\s*)|(\s*$)/g, "");
}

//单击了more图片
function image_click(){
	getInformation(20); //显示20条记录
	$("#more_").unbind("click");  //取消绑定的单击函数
}